package com.leecode;

/**
 * 206 反转链表
 */
public class Leet206 {
	public static void main(String[] args) {

	}

	public ListNode reverseList(ListNode head) {
		if (head==null) {
			return null;
		}

		if (head.next==null) {
			return new ListNode(head.val);
		}

		ListNode listNode = reverseList(head.next);

		//用指针 代替 for
		ListNode ref=listNode;
		while (ref.next != null) {
			ref=ref.next;
		}
		ref.next = new ListNode(head.val);
		return listNode;
	}

	public ListNode reverseList2(ListNode head) {
		if(head==null)return null;

		//iterate
		ListNode tail=new ListNode(head.val);//先new的Node一定是tail
		ListNode temp=tail;//初始化为null会bug,因为1->null,不进while的时候,返回temp就应该等于tail

		while(head.next!=null){
			head=head.next;
			temp=new ListNode(head.val);
			temp.next=tail;
			tail=temp;//bug

		}

		return temp;
	}
}


